<script setup lang="ts">
import {computed, onUpdated} from "vue";
import {IconType} from "@/enums/iconEnum";

interface Props {
  type?: IconType;
  name: string;
  className?: string;
}

const props = withDefaults(defineProps<Props>(), {
  type: IconType.ICON,
  name: '',
  className: '',
});

const iconName = computed(() => `#icon-${props.name}`);
const svgClass = computed(() => {
  if (props.className) {
    return "svg-icon " + props.className;
  } else {
    return "svg-icon";
  }
});
// 外链 icon
const styleExternalIcon = computed(() => {
  return {
    mask: `url(${props.name}) no-repeat 50% 50%`,
    "-webkit-mask": `url(${props.name}) no-repeat 50% 50%`
  };
});

</script>

<template>
  <div class="svg-box">
    <div
        v-if="type === IconType.EXTERNAL"
        :style="styleExternalIcon"
        class="svg-external-icon svg-icon"
        v-bind="$attrs"
    />
    <div
        v-if="type === IconType.CODE"
        class="svg-code-icon"
        v-bind="$attrs"
        v-html="name"
    />
    <svg v-if="type === IconType.ICON" :class="svgClass" aria-hidden="true" v-bind="$attrs">
      <use :xlink:href="iconName"/>
    </svg>
  </div>
</template>

<style scoped>
.svg-box {
  display: flex;
  justify-content: center;
}

.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}

.svg-external-icon {
  mask-size: cover !important;
  display: inline-block;
}
</style>
